/**
 * 封装classnames以适用于模块化的css
 * 用法
 * import s from 'sample.css';
 * import classnames from 'libs/classname-local';
 * const cx = classnames(s);
 * cx的用法同下面引用原库的cx
 * import cx from 'classnames';
 */

import classnames from 'classnames';

// 递归遍历arguments，返回一个数组
const recursive = function (args, processor) {
  return Array.prototype.map.call(args, arg => {
    switch (typeof arg) {
      case 'object':
        if (Array.isArray(arg)) { // 如果是数组
          return recursive(arg, processor);
        } else { // 如果是对象
          let obj = {};
          Object.keys(arg).forEach(name => {
            let key = processor ? processor(name) : name;
            obj[key] = arg[name];
          });
          return obj;
        }
      case 'string':
        return processor ? processor(arg) : arg;
      default:
        return arg;
    }
  });
}

export default function (local) {
  return function () {
    let args = recursive(arguments, v => v.split(/\s+/).map(s => local[s] || s));
    return classnames.apply(null, args);
  }
}
